﻿using ManagementCMS.Tool.Log4;
using Senparc.Weixin.MP.TenPayLibV3;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Web;
using Weixin.Core.Model;

namespace Weixin.Core
{
    /// <summary>
    /// 订单支付异步结果实体
    /// </summary>
    public class NotifyOrder
    {

        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        public static WxNotifyOrder NotifyResponseHandler()
        {

            WxNotifyOrder notifyOrder = new WxNotifyOrder();
            ResponseHandler notifyResponse = new ResponseHandler(null);

            //返回的参数
            string return_code = notifyResponse.GetParameter("return_code");//返回状态码
            string return_msg = notifyResponse.GetParameter("return_msg");//返回信息
            string result_code = notifyResponse.GetParameter("result_code");


            notifyOrder.out_trade_no = notifyResponse.GetParameter("out_trade_no");//商户订单号

            //验证请求是否从微信发过来（安全）
            if (return_code.ToUpper() == "SUCCESS" && result_code.ToUpper() == "SUCCESS")
            {
                //微信支付订单号
                notifyOrder.transaction_Id = notifyResponse.GetParameter("transaction_id");
                notifyResponse.SetParameter("return_msg", "OK");
                notifyResponse.SetParameter("return_code", "SUCCESS");
            }
            else
            {
                Log4Helper.WriteInfo("订单签名失败:" + notifyOrder.out_trade_no);
                //错误的订单处理
                notifyResponse.SetParameter("return_code", "FAIL");
                notifyResponse.SetParameter("return_msg", "交易失败");
            }

            return notifyOrder;
        }
    }
}
